package com.dks.repository.impl.publicCompoment;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dks.businessUtil.QsMailUtil;
import com.dks.dto.BaseIdRequestDto;
import com.dks.dto.publicCompoment.PublicCommentDto;
import com.dks.dto.publicCompoment.PublicCommentUserDto;
import com.dks.enumeration.PublicCommentEntityType;
import com.dks.mapper.PublicCommentMapper;
import com.dks.mapper.PublicCommentReplyMapper;
import com.dks.mapper.UsersMapper;
import com.dks.models.QsMail;
import com.dks.models.Users;
import com.dks.models.publicCompoment.PublicComment;
import com.dks.repository.IPublicCommentRepository;
import com.dks.result.*;
import com.dks.util.PageInfo;
import com.dks.vo.publicCompoment.PublicCommentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Repository
public class PublicCommentRepository implements IPublicCommentRepository {

    @Autowired
    private PublicCommentMapper publicCommentMapper;
    @Autowired
    private PublicCommentReplyMapper publicCommentReplyMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public ResultModel<PublicComment> add(PublicCommentDto commentDto) {
        PublicComment publicComment = new PublicComment();
        publicComment.setCommentEntityType(commentDto.getPublicCommentEntityType().name());
        publicComment.setCommentEntityId(commentDto.getCommentEntityId());
        publicComment.setComment(commentDto.getComment());
        publicComment.setCreateUserId(commentDto.getUserId());
        publicComment.setCommentType(commentDto.getCommentType());
        int result = publicCommentMapper.insert(publicComment);
        if (result <= 0){
            return Result.makeResult(ResultInfo.ERR_COMMENT_001);
        }else{

            if ("STUDENT_TASK".equals(commentDto.getPublicCommentEntityType())) {
                //作业回复添加站内信
                Users users = usersMapper.select(commentDto.getUserId());
                String nickName = "";
                if (null != users) {
                    nickName = users.getNickname();
                }

                QsMail qsMail = null;
                if (0 == users.getUserType()) {
                    //家长类型
                    qsMail = new QsMail(commentDto.getUserId(), 16, nickName, "点评了您的作业，快去看看吧", commentDto.getCommentEntityId());

                }
                QsMailUtil qsMailUtil = new QsMailUtil();
                qsMailUtil.insertQsMail(qsMail);
            }

            return Result.makeResult(ResultInfo.SUCCESS, publicCommentMapper.select(publicComment.getId()));
        }
    }

    @Override
    public ResultModel<PageInfo<PublicCommentVo>> getListByEntity(PublicCommentEntityType publicCommentEntityType, Long entityId, Integer pageNum, Integer pageSize) {
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<PublicCommentUserDto> commentList = publicCommentMapper.selectByEntity(publicCommentEntityType.name(), entityId);
        if (commentList == null || commentList.isEmpty()){
            return Result.makeResult(ResultInfo.SUCCESS);
        }
        PageInfo<PublicCommentUserDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), commentList);

        List<PublicCommentVo> commentVoList = pageInfo.getData().stream().map(PublicCommentUserDto->{
                PublicCommentVo publicCommentVo = new PublicCommentVo();
                BeanUtils.copyProperties(PublicCommentUserDto, publicCommentVo);
                publicCommentVo.setCommentReplyList(publicCommentReplyMapper.selectByCommentId(PublicCommentUserDto.getId()));
                return publicCommentVo;
            }).collect(Collectors.toList());
        PageInfo<PublicCommentVo> newPageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), commentVoList);
        return Result.makeResult(ResultInfo.SUCCESS, newPageInfo);
    }

    @Override
    public ResultModel<Boolean> remove(BaseIdRequestDto baseIdRequestDto) {
        int result = publicCommentMapper.delete(baseIdRequestDto.getUserId(), baseIdRequestDto.getId());
        if (result <= 0){
            return Result.makeResult(ResultInfo.ERR_COMMENT_004, false);
        }else{
            return Result.makeResult(ResultInfo.SUCCESS, true);
        }
    }
}
